
#include	"GObject.hh"

GObject::GObject(sf::Image &img)
  : State(1), Img(img), id(0), focus(IGUI::MAINMENU), incr(0)
{
  this->Sprite.SetImage(this->Img);
  this->Sprite.SetPosition(0, 0);
}

GObject::GObject(sf::Image &img, int s)
  : Img(img), id(0), focus(IGUI::MAINMENU), incr(0)
{
  if (s < 1)
    s = 1;
  this->State = s;
  this->Sprite.SetImage(this->Img);
  this->Sprite.SetPosition(0, 0);
  this->setState(0);
}

void		GObject::draw(sf::RenderWindow &app) const
{
  app.Draw(this->Sprite);
}

void		GObject::setState(int n)
{
  int		w;
  int		h;
  int		y;

  w = this->Img.GetWidth();
  h = this->Img.GetHeight() / this->State;
  if (n >= this->State)
    n = this->State - 1;
  else if (n < 0)
    n = 0;
  y = n * h;
  this->Sprite.SetSubRect(sf::IntRect(1, y+1, w, h * (n + 1) - 1));
}

void		GObject::setPos(int x, int y)
{
  this->Sprite.SetPosition(x, y);
}

void		GObject::setPos(int x, int y, int w, int h)
{
  this->Sprite.SetPosition(x, y);
  this->resize(w, h);
}

void		GObject::resize(int w, int h)
{
  this->Sprite.Resize(w - 1, h - 2);
}

void		GObject::setId(int v)
{
  this->id = v;
}

int		GObject::getId(void) const
{
  return (this->id);
}

int		GObject::getX(void) const
{
  return (this->Sprite.GetPosition().x);
}

int		GObject::getY(void) const
{
  return (this->Sprite.GetPosition().y);
}

int		GObject::getW(void) const
{
  return (this->Sprite.GetSize().x);
}

int		GObject::getH(void) const
{
  return (this->Sprite.GetSize().y);
}

void		GObject::setFocus(IGUI::e_context f)
{
  this->focus = f;
}

IGUI::e_context	GObject::getFocus(void) const
{
  return (this->focus);
}

void		GObject::incrState(void)
{
  this->incr++;
  if (this->incr >= this->State)
    this->incr = 0;
  this->setState(this->incr);
}
